package com.leetcode;

import com.leetcode.common.ListNode;

import static com.leetcode.util.LinkedListUtil.createList;
import static com.leetcode.util.LinkedListUtil.printList;

/**
 * 203. 移除链表元素
 * 递归, 不一样的写法
 *
 * @author fy
 * @date 2022/4/21 19:14
 */
public class Solution203_2 {

    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;
    }

    public static void main(String[] args) {
        ListNode head = createList(new int[]{6, 1, 2, 6, 3, 4, 5, 6});
        int val = 6;

        ListNode newHead = new Solution203_2().removeElements(head, val);
        printList(newHead);
    }

}
